<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2_$scope的使用</title>
    <script src="./lib/angularjs.js"></script>
</head>

<body ng-app="myApp" >
    <div ng-controller="shopCtrl">
        <p>数量：{{shop.num}}</p>
        <p>价格：{{shop.price}}</p>
        <p>总价：{{shop.num * shop.price}}</p>
    </div>
    <button ng-click="shop.add()">add</button>
    <button ng-click="shop.reduce()">reduce</button>
</body>
<script>
    const myApp = angular.module("myApp", [])
    myApp.controller("shopCtrl", ["$scope", function ($scope) {
        console.log(this)
        $scope.shop = {
            num: 20,
            price: 300,
            add: function () { this.num = Math.min(++this.num, 25) }, // 当this.num超过25之后，只会显示25（即最大数量为25）
            reduce: function () { this.num = Math.max(--this.num, 0) }  // 当this.num少于0之后，只会显示0（即最小数量为0）
        }
    }])
</script>

</html>